//
// Created by Szper on 2018/4/16.
//
#include "SolveCenter.h"
//Decide is or not is a digit
int SolveCenter::isDigit(char ch) {
    if(ch >= '0' && ch <= '9')
        return 1;
    else
        return 0;
}
// Decide is or not is letter
int SolveCenter::isLetter(char ch) {
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        return 1;
    else
        return 0;
}
//drop comment line
void SolveCenter::preCompile(char s[], int resLength) {
    char midResource[10001];
    int count = 0;
    for(int i = 0; i < resLength; ++i){
        if(s[i] == '/' && s[i+1] == '/'){
            //drop oneline comment
            while(s[i] != '\n'){
                i++;
            }
        }
        if(s[i] == '/' && s[i+1] == '*'){
            //drop mulitiline comment
            i += 2;
            while(s[i] != '*' || s[i+1] != '/'){
                i++;
                if(s[i] == '$'){
                    printf("comment error, can't find the */ in the end\n");
                    exit(0);
                }
            }
            i+=2;
        }
        if(s[i] != '\n' && s[i] != '\t' && s[i] != '\v' && s[i] != '\r'){
            midResource[count++] = s[i];
        }

    }
    midResource[count] = '\0';
    strcpy(s, midResource);
    resLength = 0;
}
void SolveCenter::ClearToken(char *token) {
    for(int i = 0; i < 21; i++){
        token[i] = '\0';
    }
}
int SolveCenter::judgeOprator(char ch) {
    if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^' || ch == '?'){
        return 1;
    }
    else
        return 0;
}
int SolveCenter::searchReserve(char resWord[][20], char *s){
    for(int i = 0; i < 30; i++){
        if(strcmp(resWord[i], s) == 0){
            return i + 1;
        }
    }
    return -1;
}
void SolveCenter::Analysis(int syn, char *res, char *token, int &resLength) {
    int i = 0, count = 0;
    char ch; //judge
    ch = res[resLength];
    while(ch == ' '){
        resLength++;
        ch = res[resLength];
    }
    this->ClearToken(token);
    if(this->isLetter(res[resLength])){
        token[count++] = res[resLength];
        resLength++;
        while(this->isLetter(res[resLength]) || this->isDigit(res[resLength])){
            token[count++]  = res[resLength];
            resLength ++;
        }
        token[count] = '\0';
        syn = this->searchReserve(resWord, token);
        if(syn == -1){
            syn = 100;//标识符识别码
        }
        return;
    }
    else if(this->isDigit(res[resLength])){
        while(this->isDigit(res[resLength])){
            token[count++] = res[resLength];
            resLength ++;
        }
        token[count] = '\0';
        syn = 99; //常数
    }
    else if(this->judgeOprator(res[resLength])){

    }

}
SolveCenter::SolveCenter() {}
SolveCenter::~SolveCenter() {}